<template>
  <div class="select-coupon-container">
    <el-button
      type="primary"
      size="mini"
      plain
      :disabled="disabled"
      @click.stop="selectCoupon"
    >{{ btnTxt }}</el-button>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :width="dialogWidth"
      top="40px"
      custom-class="coupon-select-dialog"
      append-to-body
    >
      <div class="content">
        <div class="filter-box">
          <div class="filter-type">
            <el-select v-model="filter.type" placeholder="优惠券类型" clearable>
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
          <div class="filter-value">
            <el-input v-model="filter.title" placeholder="请输入优惠券名称" />
          </div>
          <div class="filter-btn">
            <el-button type="primary" size="mini" @click="getCouponList">查询</el-button>
          </div>
        </div>
        <div v-loading="listLoading" class="table-box">
          <el-table
            ref="multipleTable"
            :data="list"
            tooltip-effect="dark"
            style="width: 100%"
            header-cell-class-name="table-th"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              v-if="single"
              label="选择"
              align="center"
              width="45"
            >
              <template scope="scope">
                <el-radio
                  v-model="radio"
                  :label="scope.row.cid"
                  @change.native="getCurrentRow(scope.row)"
                >{{
                }}</el-radio>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              type="selection"
              :selectable="couponSelectables"
              width="45"
              align="center"
            />
            <el-table-column
              label="优惠券名称"
              width="100"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.title }}</template>
            </el-table-column>

            <el-table-column label="类型" width="80" align="center" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span v-if="row.card_type === 'REDUCE'">满减券 </span>
                <span v-else-if="row.card_type === 'CASH'">代金券 </span>
                <span v-else-if="row.card_type === 'DISCOUNT'">折扣券 </span>
                <span v-else-if="row.card_type === 'EXCHANGE'">兑换券 </span>
              </template>
            </el-table-column>
            <el-table-column
              label="优惠内容"
              width="100"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">{{ scope.row.description }}</template>
            </el-table-column>
            <el-table-column
              label="有效期"
              min-width="160"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <!-- {{ scope.row.begin_timestamp | dateFormat }} - {{ scope.row.end_timestamp | dateFormat }} -->
                {{ scope.row.period_text || '-' }}
              </template>
            </el-table-column>
            <el-table-column
              label="库存"
              width="50"
              align="center"
            >
              <template slot-scope="scope">{{ scope.row.quantity }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="page-box">
          <pagination
            v-show="total > 0"
            layout="prev, pager, next"
            :total="total"
            :page.sync="filter.page"
            :limit.sync="filter.limit"
            @pagination="getCouponList"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false"> 取消 </el-button>
        <el-button size="mini" type="primary" :loading="loading" @click="confirmSelected()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCoupons } from '@/api/coupon'
import Pagination from '@/components/Pagination'
export default {
  name: 'SelectCoupon',
  components: { Pagination },
  filters: {
    dateFormat(time) {
      if (!time) {
        return time
      }
      const data = time.split(' ')
      return data[0] || time
    },
    formatNumber(value) {
      if (typeof value !== 'number' && typeof value !== 'string') {
        return value
      }
      if (typeof value === 'string') {
        value = parseFloat(value)
      }
      return value.toFixed(2)
    }
  },
  props: {
    dialogWidth: {
      type: String,
      default() {
        return '660px'
      }
    },
    dialogTitle: {
      required: true,
      type: String,
      default() {
        return '派券'
      }
    },
    btnTxt: {
      type: String,
      default() {
        return '派券'
      }
    },
    selectedData: {
      type: Array,
      default() {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    },
    dataDisabled: {
      type: Boolean,
      default() {
        return false
      }
    },
    single: {
      type: Boolean,
      default() {
        return false
      }
    },
    loading: {
      type: Boolean,
      default() {
        return false
      }
    },
    dialogSendVisible: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      listLoading: false,
      dialogVisible: false,
      types: [
        { label: '满减券', value: 'REDUCE' },
        { label: '代金券', value: 'CASH' },
        { label: '折扣券', value: 'DISCOUNT' },
        { label: '兑换券', value: 'EXCHANGE' }
      ],
      filter: {
        page: 1,
        limit: 10,
        title: undefined,
        type: undefined,
        unexpired: 1,
        status: 1
      },
      total: 0,
      list: [],
      multipleSelection: [],
      radio: ''
    }
  },
  computed: {
  },
  mounted() {
    // this.getCouponList()
    if (this.dialogSendVisible) {
      this.selectCoupon()
    }
  },
  methods: {
    selectCoupon() {
      this.list = []
      this.dialogVisible = true
      this.getCouponList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    couponSelectables(row, index) {
      if (this.dataDisabled) {
        return row.quantity >= 1
      } else {
        return true
      }
    },
    toggleSelection(rows) {
      this.$refs.multipleTable.clearSelection()
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
          this.radio = row.cid
        })
      }
    },
    getCouponList() {
      this.listLoading = true
      getCoupons(this.filter).then((response) => {
        this.listLoading = false
        const { list, total } = response.data
        this.list = list
        this.total = total
        this.$nextTick(() => {
          const oldSelectedData = this.list.filter((listItem) => {
            const oldSelectedIndex = this.selectedData.findIndex(
              (selectDataItem) =>
                selectDataItem.cid === listItem.cid
            )
            return oldSelectedIndex >= 0
          })
          this.toggleSelection(oldSelectedData)
        })
      })
    },
    confirmSelected() {
      this.$emit('selectedConfirm', this.multipleSelection)
      this.dialogVisible = false
    },
    getCurrentRow(row) {
      this.multipleSelection = [row]
    }
  }
}
</script>

<style scoped lang="scss">
.coupon-select-dialog {
  .filter-box {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    height: 50px;
    background: #F7F7FA;
    border-radius: 4px;
    padding: 10px 16px;
    .filter-btn {
      flex: auto;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
  .content {
    .filter-box {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        .filter-type {
          margin-right: 10px;
        }
      }
      .table-box {
        margin-top: 10px;
      }
      .page-box {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        .pagination-container {
          padding: 0;
        }
      }
      .btn-box {
        margin-top: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        .total-count {
          margin-right: 20px;
        }
      }

  }
}
::v-deep .el-dialog__title {
  font-size: 14px;
  font-weight: 400;
  color: #343b46;
}
</style>
